﻿using ErrorOr;
using Gym.Application.Common.Interfaces;
using Gym.Domain.RoomAggregate;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gym.Application.Rooms.Queries.ListRooms
{
    public class ListRoomsQueryHandler : IRequestHandler<ListRoomsQuery, ErrorOr<List<Room>>>
    {
        private readonly IGymsRepository _gymsRepository;
        private readonly IRoomsRepository _roomsRepository;

        public ListRoomsQueryHandler(IGymsRepository gymsRepository, IRoomsRepository roomsRepository)
        {
            _gymsRepository = gymsRepository;
            _roomsRepository = roomsRepository;
        }

        public async Task<ErrorOr<List<Room>>> Handle(ListRoomsQuery request, CancellationToken cancellationToken)
        {
            if (!await _gymsRepository.ExistsAsync(request.GymId))
            {
                return Error.NotFound(description: "没有找到该健身房信息");
            }
            return await _roomsRepository.ListByGymIdAsync(request.GymId);
        }
    }
}
